﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Web.SessionState;

namespace HQWeb.View
{
    /// <summary>
    /// GetLink 的摘要说明
    /// </summary>
    public class GetLink : IHttpHandler, IReadOnlySessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string strType = context.Request["type"];
            if (context.Session["USER_ID"] != null)
            {
                string strUserID = context.Session["USER_ID"].ToString();
                if (strType == "view")
                {
                    context.Response.Write(GetLinks(strUserID));
                }
                else if (strType == "editview")
                {
                    context.Response.Write(GetEditLinks(strUserID));
                }
                else if (strType == "add")
                {
                    string strName = context.Request["name"];
                    string strUrl = context.Request["linkurl"];
                    Model.SysManage.T_PUB_LINK model = new Model.SysManage.T_PUB_LINK();
                    model.NAME = strName;
                    model.LINK = strUrl;
                    model.USERID = strUserID;
                    BLL.SysManage.T_PUB_LINK bll = new BLL.SysManage.T_PUB_LINK();
                    bll.Add(model);
                    context.Response.Write(GetEditLinks(strUserID));
                }
                else if (strType == "edit")
                {
                    string strName = context.Request["name"];
                    string strUrl = context.Request["linkurl"];
                    string strID = context.Request["id"];
                    Model.SysManage.T_PUB_LINK model = new Model.SysManage.T_PUB_LINK();
                    model.ID = Convert.ToDecimal(strID);
                    model.NAME = strName;
                    model.LINK = strUrl;
                    model.USERID = strUserID;
                    BLL.SysManage.T_PUB_LINK bll = new BLL.SysManage.T_PUB_LINK();
                    bll.Update(model);
                    context.Response.Write(GetEditLinks(strUserID));
                }
                else if (strType == "delted")
                {
                    string strID = context.Request["id"];
                    BLL.SysManage.T_PUB_LINK bll = new BLL.SysManage.T_PUB_LINK();
                    bll.Delete(Convert.ToInt32(strID));
                    context.Response.Write(GetEditLinks(strUserID));
                }
            }
            else
            {
                context.Response.Write("SessionTimeout");
            }
        }

        private string GetLinks(string strUserID)
        {
            StringBuilder strHtml = new StringBuilder();
            BLL.SysManage.T_PUB_LINK linkBll = new BLL.SysManage.T_PUB_LINK();
            List<Model.SysManage.T_PUB_LINK> modelList = linkBll.GetpersonLink(strUserID);
            IEnumerable<Model.SysManage.T_PUB_LINK> PublickLink = modelList.Where(p => p.USERID == null || p.USERID == "");
            IEnumerable<Model.SysManage.T_PUB_LINK> PrivaeLink = modelList.Where(p => p.USERID != null && p.USERID != "");
            string strName;
            foreach (Model.SysManage.T_PUB_LINK item in PublickLink)
            {
                strName = item.NAME.Length > 10 ? (item.NAME.Substring(0, 8) + "...") : item.NAME;
                strHtml.Append(string.Format("<span class=\"js_vtext\" ><a href=\"{0}\" target=\"_blank\">{1}</a></span>", item.LINK, strName));
            }
            strHtml.Append("<hr />");
            foreach (Model.SysManage.T_PUB_LINK item in PrivaeLink)
            {
                strName = item.NAME.Length > 10 ? (item.NAME.Substring(0, 8) + "...") : item.NAME;
                strHtml.Append(string.Format("<span class=\"js_vtext\" ><a href=\"{0}\" target=\"_blank\">{1}</a></span>", item.LINK, strName));
            }
            strHtml.Append("<div style=\"clear: both\">  <input type=\"button\" value=\"编辑\" onclick=\"showEdit()\" />");

            return strHtml.ToString();
        }
        private string GetEditLinks(string strUserID)
        {
            StringBuilder strHtml = new StringBuilder();
            strHtml.Append(" <div> <ul class=\"js_psite-sites\">");
            BLL.SysManage.T_PUB_LINK linkBll = new BLL.SysManage.T_PUB_LINK();
            List<Model.SysManage.T_PUB_LINK> modelList = linkBll.GetpersonLink(strUserID);
            string strName = "";
            foreach (Model.SysManage.T_PUB_LINK item in modelList)
            {
                strName = item.NAME.Length > 10 ? (item.NAME.Substring(0, 8) + "...") : item.NAME;
                if (string.IsNullOrEmpty(item.USERID))
                {
                    strHtml.Append(string.Format("<li data-url=\"{0}\"  ><span onclick=\"showLinkPage(this)\" class=\"js_text\">{1}</span></li>", item.LINK, strName));
                }
                else
                {
                    strHtml.Append(string.Format("<li data-url=\"{0}\"   class=\"js_site-item\">", item.LINK));
                    strHtml.Append(string.Format("<span class=\"js_text\" onclick=\"showLinkPage(this)\" title='{0}'>{1}</span>", item.NAME, strName));
                    strHtml.Append("<a hidefocus=\"true\" title=\"修改\" dataid=\"" + item.ID + "\" onclick=\"fedit(this)\"  class=\"js_modify\"></a>");
                    strHtml.Append("<a hidefocus=\"true\" title=\"删除\" dataid=\"" + item.ID + "\"  onclick=\"fLinkDel(this)\" class=\"js_delete\"></a></li>");
                }
            }
            strHtml.Append(" </ul></div>");
            strHtml.Append("<div style=\"clear: both\">网站名称：<input class=\"js_sug-name name\" id=\"js_sug-name\" autocomplete=\"off\"/>");
            strHtml.Append("网址：<input class=\"js_sug-url url\" id=\"js_sug-url\" autocomplete=\"off\"/>  <input type=\"button\" value=\"保存\" id='addLink' onclick=\"funSaveLink('add')\"/><input type=\"button\" value=\"取消\" id='CanleLink' onclick=\"showLinkView()\"/> </div>");
            return strHtml.ToString();
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}